package org.boot.ssm.wxshop.service.impl;

import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.Comment;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.mapper.CommentMapper;
import org.boot.ssm.wxshop.service.CommentService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @program: wxshop
 * @description: 评论业务实现类
 * @author: Mr.Wang
 * @create: 2018-03-22 15:36
 **/

@Service
public class CommentServiceImpl extends BaseServiceImpl<Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;


    /**
     * 添加用户评论
     *
     * @param comment
     * @param user
     * @return
     */
    @Override
    public JsonResult addComment(Comment comment, User user) {
        comment.setUserid(user.getId());
        Integer isComm;
        isComm = this.saveNoNull(comment);
        if (isComm == 1) {
            return new JsonResult(200, "评论成功");
        } else {
            throw new BusinessException("评论失败");
        }
    }

    /**
     * 根据商品ID查询评论和用户名集合
     * @param goodsId
     * @return
     */
    @Override
    public JsonResult queryCommentAndUserNameListByGoodsId(Integer goodsId) {
        return new JsonResult(200,"加载成功",commentMapper.queryCommentAndUserNameListByGoodsId(goodsId));
    }
}
